using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Drawing.Design;
using Raider.WinForms.Actions.Properties;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms;
using System;

namespace Raider.WinForms.Actions.Standard
{
    /// <summary>
    /// Displays information about the application.
    /// </summary>
    [Description("Displays information about the application.")]
    public class AboutAction : StandardAction
    {
        private readonly AboutForm aboutForm = new AboutForm();
        private bool initialized;

        public AboutAction()
            : base(Resources.About, "Help", Keys.None, null)
        {
            AdditionalText = "";
            LicenseText = "";
        }

        /// <summary>
        /// Defines additional text, displayed below the version.
        /// </summary>
        [DefaultValue("")]
        [Category("Appearance"), Description("Defines additional text, displayed below the version.")]
        [Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design", typeof(UITypeEditor))]
        public string AdditionalText
        {
            get;
            set;
        }

        /// <summary>
        /// Defines a license text, displayed on the RichTextBox.
        /// </summary>
        [DefaultValue("")]
        [Category("Appearance"), Description("Defines a license text, displayed on the rich text box.")]
        [Editor("System.ComponentModel.Design.MultilineStringEditor, System.Design", typeof(UITypeEditor))]
        public string LicenseText
        {
            get;
            set;
        }

        /// <summary>
        /// <see cref="AboutAction"/> does not support asynchronous execution.
        /// </summary>
        /// <exception cref="InvalidOperationException">When trying to set this property to <c>true</c>.</exception>
        [Description("This action does not support asynchronous execution.")]
        public sealed override bool AsyncExecute
        {
            get { return base.AsyncExecute; }
            set
            {
                if (value)
                {
                    throw new InvalidOperationException(GetType() + " does not support asynchronous execution.");
                }
            }
        }

        protected override void OnExecute(ExecuteEventArgs args)
        {
            base.OnExecute(args);

            if (!initialized)
            {
                var entryAssembly = Assembly.GetEntryAssembly();
                var fileInfo = FileVersionInfo.GetVersionInfo(entryAssembly.Location);

                aboutForm.Application = fileInfo.ProductName;
                aboutForm.Author = fileInfo.CompanyName;
                aboutForm.Copyright = fileInfo.LegalCopyright;
                aboutForm.Version = fileInfo.ProductVersion;
                aboutForm.AdditionalText = AdditionalText;
                aboutForm.LicenseText = LicenseText;

                var icon = Icon.ExtractAssociatedIcon(entryAssembly.Location);
                if (icon != null)
                {
                    aboutForm.Logo = icon.ToBitmap();
                    icon.Dispose();
                }

                initialized = true;
            }

            aboutForm.ShowDialog();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                aboutForm.Dispose();
            }

            base.Dispose(disposing);
        }
    }
}